package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 步骤：创建监听器，监听器拥有相应触发的动作，组件添加入监听
 * 组件可以设置成类属性
 * 测试鼠标进入组件和退出组件的样式变化
 */
public class MouseEventDemo extends JFrame implements ActionListener {
    JButton button = new JButton("登录");

    public MouseEventDemo() {
        this.setSize(800, 600);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        Dimension dim = new Dimension(80, 60);

        button.setPreferredSize(dim);

        button.setBounds(100, 100, 200, 100);
        button.addMouseListener(new ButtonMouseAction(button));
        button.addActionListener(this);
        this.add(button);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        button.setFont(new Font("黑体", Font.BOLD, 28));

        if (e.getActionCommand().equals("登录")) {
            System.out.println("actionListener");
        }
    }

    class ButtonMouseAction extends MouseAdapter {
        JButton but;  //需要发生动作的组件对象

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("鼠标进入控件");
            Object eSource = e.getSource();
            System.out.println(eSource.toString());
            but.setBackground(Color.cyan);

            System.out.println(e.paramString());
        }

        @Override
        public void mouseExited(MouseEvent e) {
            but.setBackground(null);
        }

        ButtonMouseAction(JButton button) {
            this.but = button;
        }
    }


    public static void main(String[] args) {
        MouseEventDemo mouseEventDemo = new MouseEventDemo();
    }


}
